import axios from 'axios'
import {
	baseUrl
} from './env'

// 基础配置
const ins = axios.create({
  baseURL: baseUrl,
  // 设置请求超时的时长
  timeout: 6000
})

// 请求拦截
ins.interceptors.request.use((config) => {
    // 在请求头中设置新属性token，参数为我们的token ，实现每次请求都会在请求头中携带token
  config.headers.common.token = localStorage.getItem('token') || ''
  return config
}, error => {
  return Promise.reject(error)
})

// 响应拦截
ins.interceptors.response.use(response => {
// 根据后端返回的数据，错误时的统一操作
  if (response.data.code === '10119') {
    window.location.href = window.location.href.split('#')[0] + '#' + '/login'
    window.location.reload()
  } 
  console.log(response)
    return response
}, error => {
  return Promise.reject(error)
})

    
function request(config) {
  let { url, data, method, headers } = config
  url = url || ''
  data = data || {}
  method = method || 'GET'
  headers = headers || ''

  // restful api  GET / POST /PUT/PATCH/DELETE
  switch (method.toUpperCase()) {
    case 'GET':
      return ins.get(url, { params: data })
    case 'POST':
      // 表单提交  application/x-www-form-url-encoded
      if (headers['content-type'] === 'application/x-www-form-url-encoded') {
        // 转参数 URLSearchParams/第三方库qs
        const p = new URLSearchParams()
        for (let key in data) {
          p.append(key, data[key])
        }
        return ins.post(url, p, { headers })
      }
      // 文件提交  multipart/form-data
      if (headers['content-type'] === 'multipart/form-data') {
        const p = new FormData()
        for (let key in data) {
          p.append(key, data[key])
        }
        return ins.post(url, p, { headers })
      }
      // 默认 application/json
      return ins.post(url, data)
    case 'PUT': // 全部更新
      return ins.put(url, data)
    case 'PATCH': // 局部更新
      return ins.patch(url, data)
    case 'DELETE': // 删除
      return ins.delete(url, { data })
    default:
      return ins(config)
  }
}

export default request